package com.baseproject.widget;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.ColorRes;
import android.support.annotation.DimenRes;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baseproject.R;

/**
 * Created by hp on 2017/5/18.
 */

public class AlertDialog extends SimpleDialog {

    private String title;
    private String content;
    private TextView tvTitle;
    private TextView tvContent;
    private LinearLayout llBottom;
    private TextView positiveButton;
    private int positiveButtonBgColor = -1;
    private TextView negativeButton;
    private int negativeButtonBgColor = -1;
    private TextView button;
    private int buttonBgColor = -1;
    private TextView button1;
    private int button1BgColor = -1;
    private TextView button2;
    private int button2BgColor = -1;
    private LinearLayout lineTitle;
    private LinearLayout lineContent;
    private int buttonMiniHeight = 0;
    private int buttonTextSize = 0;
    private boolean isBottomLine = true;

    public AlertDialog _setCanceledOnTouchOutside(boolean bool) {
        super.setCanceledOnTouchOutside(bool);
        return this;
    }

    public AlertDialog _setCancelable(boolean bool) {
        super.setCancelable(bool);
        return this;
    }

    public AlertDialog _setDividerColor(int resId) {
        super.setDividerColor(resId);
        return this;
    }

    public AlertDialog _setTitle(String title) {
        super.setTitle(title);
        return this;
    }

    public AlertDialog _setPositionButtonOnClickListener(View.OnClickListener onClickListener) {
        positiveButton.setOnClickListener(onClickListener);
        return this;
    }

    public AlertDialog _setOnDismissListener(OnDismissListener onDismissListener) {
        super.setOnDismissListener(onDismissListener);
        return this;
    }

    public AlertDialog _setPercentWith(float percentWidth) {
        super.setPercentWith(percentWidth);
        return this;
    }

    public AlertDialog(Context context) {
        super(context);
        this.buttonMiniHeight = context.getResources().getDimensionPixelSize(R.dimen.dialog_btnMiniHeight);
        this.buttonTextSize = context.getResources().getDimensionPixelSize(R.dimen.dialog_button_textSize);
    }

    protected void initView() {
        this.tvTitle = (TextView)this.getViewById(R.id.AlertDialog_tvTitle);
        this.lineTitle = (LinearLayout)this.getViewById(R.id.AlertDialog_lineTitle);
        this.lineContent = (LinearLayout)this.getViewById(R.id.AlertDialog_lineContent);
        this.tvContent = (TextView)this.getViewById(R.id.AlertDialog_tvContent);
        this.llBottom = (LinearLayout)this.getViewById(R.id.AlertDialog_llBottom);
    }

    public AlertDialog build() {
        this.lineTitle.removeAllViews();
        this.lineTitle.addView(this.getVerticalLineView());
        this.lineContent.removeAllViews();
        this.lineContent.addView(this.getVerticalLineView());
        int bottomNum = 0;
        if(this.positiveButton != null) {
            ++bottomNum;
        }

        if(this.negativeButton != null) {
            ++bottomNum;
        }

        if(this.button != null) {
            ++bottomNum;
        }

        if(this.button1 != null) {
            ++bottomNum;
        }

        if(this.button2 != null) {
            ++bottomNum;
        }

        int bottomIndex = 0;
        if(this.negativeButton != null) {
            this.llBottom.addView(this.negativeButton);
            this.setButtonBackground(this.negativeButton, bottomIndex, bottomNum, this.negativeButtonBgColor);
            ++bottomIndex;
        }

        if(this.button != null) {
            if(this.isBottomLine && bottomIndex > 0 && bottomNum > 0) {
                this.llBottom.addView(this.getHorizontalLineView());
            }

            this.llBottom.addView(this.button);
            this.setButtonBackground(this.button, bottomIndex, bottomNum, this.buttonBgColor);
            ++bottomIndex;
            --bottomNum;
        }

        if(this.button1 != null) {
            if(this.isBottomLine && bottomIndex > 0 && bottomNum > 0) {
                this.llBottom.addView(this.getHorizontalLineView());
            }

            this.llBottom.addView(this.button1);
            this.setButtonBackground(this.button1, bottomIndex, bottomNum, this.button1BgColor);
            ++bottomIndex;
            --bottomNum;
        }

        if(this.button2 != null) {
            if(this.isBottomLine && bottomIndex > 0 && bottomNum > 0) {
                this.llBottom.addView(this.getHorizontalLineView());
            }

            this.llBottom.addView(this.button2);
            this.setButtonBackground(this.button2, bottomIndex, bottomNum, this.button2BgColor);
            ++bottomIndex;
            --bottomNum;
        }

        if(this.positiveButton != null) {
            if(this.isBottomLine && bottomIndex > 0 && bottomNum > 0) {
                this.llBottom.addView(this.getHorizontalLineView());
            }
            this.llBottom.addView(this.positiveButton);
            this.setButtonBackground(this.positiveButton, bottomIndex, bottomNum, this.positiveButtonBgColor);
            --bottomNum;
        }
        return this;
    }

    public TextView setTitle(String title) {
        this.title = title;
        this.tvTitle.setText(title);
        return this.tvTitle;
    }

    public AlertDialog _show() {
        super.show();
        return this;
    }

    public TextView getTitle() {
        return this.tvTitle;
    }

    public AlertDialog setTitleTextSize(@DimenRes int res) {
        this.tvTitle.setTextSize(0, this.context.getResources().getDimension(res));
        return this;
    }

    public AlertDialog setTitleTextColor(@ColorRes int resColor) {
        this.tvTitle.setTextColor(this.context.getResources().getColor(resColor));
        return this;
    }

    public AlertDialog setContent(String content) {
        this.content = content;
        this.tvContent.setText(content);
        return this;
    }

    public TextView getContent() {
        return this.tvContent;
    }

    public AlertDialog setContentTextSize(@DimenRes int res) {
        this.tvContent.setTextSize(0, this.context.getResources().getDimension(res));
        return this;
    }

    public TextView setContentTextColor(@ColorRes int resColor) {
        this.tvContent.setTextColor(this.context.getResources().getColor(resColor));
        return this.tvContent;
    }

    public TextView setContentCenter() {
        this.tvContent.setGravity(17);
        return this.tvContent;
    }

    public void hideTitle() {
        this.tvTitle.setVisibility(View.GONE);
        this.lineTitle.setVisibility(View.GONE);
    }

    public void hideContent() {
        this.tvContent.setVisibility(View.GONE);
        this.lineContent.setVisibility(View.GONE);
    }

    public void hideTitleLine() {
        this.lineTitle.setVisibility(View.GONE);
    }

    public void hideContentLine() {
        this.lineContent.setVisibility(View.GONE);
    }

    public void hideLine() {
        this.lineTitle.setVisibility(View.GONE);
        this.lineContent.setVisibility(View.GONE);
    }

    public void setButtonBackground(TextView button, int index, int buttonCount, int color) {
        if(buttonCount == 1) {
            button.setBackgroundDrawable(this.getGradientDrawableBottomLeftRight(color));
        } else if(buttonCount > 1 && index == 0) {
            button.setBackgroundDrawable(this.getGradientDrawableBottomLeft(color));
        } else if(buttonCount > 1 && index == buttonCount - 1) {
            button.setBackgroundDrawable(this.getGradientDrawableBottomRight(color));
        } else {
            button.setBackgroundDrawable(this.getGradientDrawable(color));
        }

    }

    public void setDefaultButtonMiniHeight(@DimenRes int res) {
        this.buttonMiniHeight = this.context.getResources().getDimensionPixelSize(res);
    }

    public void setDefaultButtonTextSize(@DimenRes int res) {
        this.buttonTextSize = this.context.getResources().getDimensionPixelSize(res);
    }

    public AlertDialog setPositiveButton(String text, @ColorRes int textColorRes, @ColorRes int backgroundRes) {
        this.setPositiveButton(text, textColorRes);
        this.positiveButtonBgColor = this.context.getResources().getColor(backgroundRes);
        return this;
    }

    public TextView setPositiveButton(String text, @ColorRes int textColorRes) {
        this.setPositiveButton(text);
        this.positiveButton.setTextColor(this.context.getResources().getColor(textColorRes));
        return this.positiveButton;
    }

    public TextView setPositiveButton(String text) {
        if(this.positiveButton == null) {
            this.positiveButton = new TextView(this.context);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(-1, -2, 1.0F);
            this.positiveButton.setLayoutParams(lp);
            this.positiveButton.setMinHeight(this.buttonMiniHeight);
            this.positiveButton.setGravity(17);
        }

        this.positiveButton.setText(text);
        this.positiveButton.setTextSize(0, (float)this.buttonTextSize);
        return this.positiveButton;
    }

    public TextView setPositiveButtonBackground(@ColorRes int res) {
        this.positiveButtonBgColor = this.context.getResources().getColor(res);
        return this.positiveButton;
    }

    public TextView setPositiveButtonTextColor(@ColorRes int res) {
        this.positiveButton.setTextColor(this.context.getResources().getColor(res));
        return this.positiveButton;
    }

    public TextView setPositiveButtonTextSize(@DimenRes int res) {
        this.positiveButton.setTextSize(0, this.context.getResources().getDimension(res));
        return this.positiveButton;
    }

    public TextView setNegativeButton(String text, @ColorRes int textColorRes, @ColorRes int backgroundRes) {
        this.setNegativeButton(text, textColorRes);
        this.negativeButtonBgColor = this.context.getResources().getColor(backgroundRes);
        return this.negativeButton;
    }

    public TextView setNegativeButton(String text, @ColorRes int textColorRes) {
        this.setNegativeButton(text);
        this.negativeButton.setTextColor(this.context.getResources().getColor(textColorRes));
        return this.negativeButton;
    }

    public TextView setNegativeButton(String text) {
        if(this.negativeButton == null) {
            this.negativeButton = new TextView(this.context);
            this.negativeButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    AlertDialog.this.dismiss();
                }
            });
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(-1, -2, 1.0F);
            this.negativeButton.setLayoutParams(lp);
            this.negativeButton.setMinHeight(this.buttonMiniHeight);
            this.negativeButton.setGravity(17);
        }

        this.negativeButton.setText(text);
        this.negativeButton.setTextSize(0, (float)this.buttonTextSize);
        return this.negativeButton;
    }

    public TextView setNegativeButtonBackground(@ColorRes int res) {
        this.negativeButtonBgColor = this.context.getResources().getColor(res);
        return this.negativeButton;
    }

    public TextView setNegativeButtonTextColor(@ColorRes int res) {
        this.negativeButton.setTextColor(this.context.getResources().getColor(res));
        return this.negativeButton;
    }

    public TextView setNegativeButtonTextSize(@DimenRes int res) {
        this.negativeButton.setTextSize(0, this.context.getResources().getDimension(res));
        return this.negativeButton;
    }

    public TextView setButton(String text, @ColorRes int textColorRes, @ColorRes int backgroundRes) {
        this.setButton(text, textColorRes);
        this.buttonBgColor = this.context.getResources().getColor(backgroundRes);
        return this.button;
    }

    public TextView setButton(String text, @ColorRes int textColorRes) {
        this.setButton(text);
        this.button.setTextColor(this.context.getResources().getColor(textColorRes));
        return this.button;
    }

    public TextView setButton(String text) {
        if(this.button == null) {
            this.button = new TextView(this.context);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(-1, -2, 1.0F);
            this.button.setLayoutParams(lp);
            this.button.setMinHeight(this.buttonMiniHeight);
            this.button.setGravity(17);
        }

        this.button.setText(text);
        this.button.setTextSize(0, (float)this.buttonTextSize);
        this.button.setBackgroundDrawable(this.getGradientDrawable(Color.argb(100, 255, 255, 255)));
        return this.button;
    }

    public TextView setButtonBackground(@ColorRes int res) {
        this.buttonBgColor = this.context.getResources().getColor(res);
        return this.button;
    }

    public TextView setButtonTextColor(@ColorRes int res) {
        this.button.setTextColor(this.context.getResources().getColor(res));
        return this.button;
    }

    public TextView setButtonTextSize(@DimenRes int res) {
        this.button.setTextSize(0, this.context.getResources().getDimension(res));
        return this.button;
    }

    public TextView setButton1(String text, @ColorRes int textColorRes, @ColorRes int backgroundRes) {
        this.setButton1(text, textColorRes);
        this.button1BgColor = this.context.getResources().getColor(backgroundRes);
        return this.button1;
    }

    public TextView setButton1(String text, @ColorRes int textColorRes) {
        this.setButton1(text);
        this.button1.setTextColor(this.context.getResources().getColor(textColorRes));
        return this.button1;
    }

    public TextView setButton1(String text) {
        if(this.button1 == null) {
            this.button1 = new TextView(this.context);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(-1, -2, 1.0F);
            this.button1.setLayoutParams(lp);
            this.button1.setMinHeight(this.buttonMiniHeight);
            this.button1.setGravity(17);
        }

        this.button1.setText(text);
        this.button1.setTextSize(0, (float)this.buttonTextSize);
        this.button1.setBackgroundDrawable(this.getGradientDrawable(Color.argb(100, 255, 255, 255)));
        return this.button1;
    }

    public TextView setButton1Background(@ColorRes int res) {
        this.button1BgColor = this.context.getResources().getColor(res);
        return this.button1;
    }

    public TextView setButton1TextColor(@ColorRes int res) {
        this.button1.setTextColor(this.context.getResources().getColor(res));
        return this.button1;
    }

    public TextView setButton1TextSize(@DimenRes int res) {
        this.button1.setTextSize(0, this.context.getResources().getDimension(res));
        return this.button1;
    }

    public TextView setButton2(String text, @ColorRes int textColorRes, @ColorRes int backgroundRes) {
        this.setButton2(text, textColorRes);
        this.button2BgColor = this.context.getResources().getColor(backgroundRes);
        return this.button2;
    }

    public TextView setButton2(String text, @ColorRes int textColorRes) {
        this.setButton2(text);
        this.button2.setTextColor(this.context.getResources().getColor(textColorRes));
        return this.button2;
    }

    public TextView setButton2(String text) {
        if(this.button2 == null) {
            this.button2 = new TextView(this.context);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(-1, -2, 1.0F);
            this.button2.setLayoutParams(lp);
            this.button2.setMinHeight(this.buttonMiniHeight);
            this.button2.setGravity(17);
        }

        this.button2.setText(text);
        this.button2.setTextSize(0, (float)this.buttonTextSize);
        this.button2.setBackgroundDrawable(this.getGradientDrawable(Color.argb(100, 255, 255, 255)));
        return this.button2;
    }

    public TextView setButton2Background(@ColorRes int res) {
        this.button2BgColor = this.context.getResources().getColor(res);
        return this.button2;
    }

    public TextView setButton2TextColor(@ColorRes int res) {
        this.button2.setTextColor(this.context.getResources().getColor(res));
        return this.button2;
    }

    public TextView setButton2TextSize(@DimenRes int res) {
        this.button2.setTextSize(0, this.context.getResources().getDimension(res));
        return this.button2;
    }

    protected int getDialogLayoutId() {
        return R.layout.dialog_dialog_alert;
    }

}
